package yja.gzw.app.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import yja.gzw.app.R
import yja.gzw.app.activity.XiaoxixiangqingActivity
import yja.gzw.app.model.bean.MessageDataxt
import java.time.Instant
import java.time.ZoneId
import java.time.format.DateTimeFormatter


class XiaoxiAdapter (private val context: Context, private val list: List<MessageDataxt>): RecyclerView.Adapter<RecyclerView.ViewHolder>(){

    private var view: View?=null
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        view= LayoutInflater.from(parent.context).inflate(R.layout.item_view_xitongxiaoxi,parent,false)

        return ViewHodeView(view!!)
    }

    override fun getItemCount(): Int {
       return list.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
       if(holder is ViewHodeView){

           holder.neirong.text=list[position].content
           val timestamp = list[position].createtime
           // 将秒级时间戳转换为 Instant 对象
           //todo 爆红不必理会
           val instant = Instant.ofEpochSecond(timestamp)

           val zoneId = ZoneId.systemDefault()
           // 将 Instant 对象转换为 LocalDateTime 对象
           val localDateTime = instant.atZone(zoneId).toLocalDateTime()
           // 定义日期时间格式化模式
           val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
           // 格式化日期时间
           val formattedDateTime = localDateTime.format(formatter)

           holder.shijian.text=formattedDateTime


           holder.itemView.setOnClickListener {
               val intent=Intent(context, XiaoxixiangqingActivity::class.java)
               intent.putExtra("xiaoxi", list[position].content)

               context.startActivity(intent)
           }


       }
    }

    inner class ViewHodeView(itemView: View):RecyclerView.ViewHolder(itemView){
        val neirong: TextView =itemView.findViewById(R.id.name)
        val shijian: TextView =itemView.findViewById(R.id.chujianqian)

    }}